﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RFIDStockManagement.Common.Public.InterFace;

namespace RFIDStockManagement.Client.Report
{
    public delegate void ReportBarEvent(object sender,string barName,ref bool isCanceled);

    /// <summary>
    /// 报表插件
    /// </summary>
    public class ReportPlugIn 
    {
        private List<ReportBarItem> _bars;
        public ReportBarEvent OnBarClick;
        private ReportControl _owner;
        private string _reportHeader= string.Empty;
        private IReportFilter _thisFilter;

        public ReportPlugIn()
        {
            _bars = new List<ReportBarItem>();
        }

        /// <summary>
        /// 当前的报表控件
        /// </summary>
        public ReportControl Owner
        {
            get { return _owner; }
            set { _owner = value; }
        }
        /// <summary>
        /// 报表的打印标题
        /// </summary>
        public string ReportHeader
        {
            get { return _reportHeader; }
            set { _reportHeader = value; }
        }

        /// <summary>
        /// 过滤接口
        /// </summary>
        public IReportFilter Filter
        {
            get { return _thisFilter; }
            set { _thisFilter = value; }
        }

        /// <summary>
        /// 获取自定义按钮
        /// </summary>
        public virtual List<ReportBarItem> Bars
        {
            get { return _bars; }
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="key"></param>
        /// <param name="caption"></param>
        /// <param name="imageKey"></param>
        /// <param name="visible"></param>
        public virtual void AddBar(string key, string caption, string imageKey, bool visible)
        {
            foreach (ReportBarItem x in _bars)
            {
                if (x.Key == key.Trim().ToLower())
                {
                    x.Caption = caption;
                    x.ImgKey = imageKey;
                    x.Visible = visible;
                    return;
                }
            }
            ReportBarItem newBar = new ReportBarItem();
            newBar.Key = key.Trim().ToLower();
            newBar.ImgKey = imageKey;
            newBar.Caption = caption;
            newBar.Visible = visible;

            _bars.Add(newBar);
        }

        /// <summary>
        /// 启用或禁用工具条
        /// </summary>
        /// <param name="isEnabled"></param>
        public virtual void EnableAllBar(bool isEnabled)
        {
            if (Owner == null) return;

            Owner.EnableBar(isEnabled);
        }

        public virtual void EnableBar(string barName, bool enabled)
        {
            if (Owner == null) return;

            Owner.EnableBar(barName, enabled);
        }

        /// <summary>
        /// 刷新报表数据
        /// </summary>
        public void RefreshReportData()
        {
            if (Owner != null)
            {
                Owner.RefreshData();
            }
        }

        /// <summary>
        /// 展示过滤界面
        /// </summary>
        /// <returns>true获取过滤条件OK,false获取过滤条件失败</returns>
        public bool ShowFilter(string parms)
        {
            if (_thisFilter == null) return false;

            return _thisFilter.Show(parms);
        }

    }

    /// <summary>
    /// 用于定义报表的bar
    /// </summary>
    public class ReportBarItem
    {
        public string Key;
        public string Caption;
        public string ImgKey;
        public bool Visible;
    }
}
